﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

using Microsoft.Xna.Framework.Graphics;

using Color = Microsoft.Xna.Framework.Color;
using Rectangle = Microsoft.Xna.Framework.Rectangle;
using System.Diagnostics;



namespace HorrorsOfArkham.Game_Application_Layer
{
    public class MyXnaGame : TimedXnaGame
    {
        #region Members

        private readonly IRenderView _renderView;
        private Rectangle _destinationRectangle;
        private Texture2D _onePixelTexture;

        private const float OPTIMAL_DELTA_TIME = 1.0f / 60.0f * 1000;
        private const float MAX_FRAME_TIME = 15f;

        #endregion

        #region Initialization

        public MyXnaGame(GraphicsDevice graphicsDevice, IContentManagerProvider contentManagerProvider, IRenderView renderView)
            : base(graphicsDevice, contentManagerProvider)
        {
            _renderView = renderView;
        }

        protected override void LoadContent()
        {
            var onePixelBitmap = new Bitmap(1, 1, PixelFormat.Format32bppArgb);

            using (Graphics graphics = Graphics.FromImage(onePixelBitmap))
            {
                graphics.FillRectangle(Brushes.White, 0, 0, 1, 1);
            }

            using (var stream = new MemoryStream())
            {
                onePixelBitmap.Save(stream, ImageFormat.Png);
                _onePixelTexture = Texture2D.FromStream(GraphicsDevice, stream);
            }
        }

        #endregion

        #region Destruction

        protected override void OnDispose(bool disposing)
        {
            if (disposing)
            {
                _onePixelTexture.Dispose();
            }
        }

        #endregion

        #region Update

        protected override void Update()
        {
            if (!Paused)
            {
                Update(base._gameTime);
            }
        }

        protected override void Update(IXnaGameTime gameTime)
        {
            TimeSpan lastFrameTime = gameTime.ElapsedGameTime;

            float deltaTime = 0;

            if (lastFrameTime.Milliseconds > MAX_FRAME_TIME)
            {
                deltaTime = OPTIMAL_DELTA_TIME;
            }
            else
            {
                deltaTime = lastFrameTime.Milliseconds;
            }

            float factor = DateTime.Now.Millisecond / 1000f;

            _destinationRectangle = new Rectangle(0, 0, (int)(_renderView.ViewSize.Width * factor), (int)(_renderView.ViewSize.Height * factor));

            Console.WriteLine("Delta time: " + deltaTime);
        }

        #endregion

        #region Draw

        protected override void Draw()
        {
            using (var spriteBatch = new SpriteBatch(GraphicsDevice))
            {
                spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);

                spriteBatch.Draw(_onePixelTexture, _destinationRectangle, Color.Yellow);

                spriteBatch.End();
            }
        }

        protected override void Present()
        {
            GraphicsDevice.Present(new Rectangle(0, 0, _renderView.ViewSize.Width, _renderView.ViewSize.Height), null, IntPtr.Zero);
        }

        #endregion
    }
}
